<template>
  <div class="login">
    <div class="cyber-background">
      <div class="grid"></div>
      <div class="lines"></div>
      <div class="glow"></div>
    </div>

    <div class="login-container">
      <div class="login-logo">
        <h1 class="cyber-title">asaaaa<span class="blink">_</span></h1>
        <div class="subtitle">智能制造执行系统</div>
      </div>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <h3 class="title">系统登录</h3>
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            auto-complete="off"
            placeholder="账号"
          >
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleLogin"
          >
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>

        <!-- 其他表单项保持不变 -->
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            class="cyber-button"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right;" v-if="register">
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024-2025 CFL All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
      },
      loading: false,
      captchaOnOff: false,
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  position: relative;
  overflow: hidden;

  .cyber-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: #0a0a1a;
    z-index: -1;

    .grid {
      position: absolute;
      width: 200%;
      height: 200%;
      top: -50%;
      left: -50%;
      background-image:
        linear-gradient(rgba(0, 255, 255, 0.2) 1px, transparent 1px),
        linear-gradient(90deg, rgba(0, 255, 255, 0.2) 1px, transparent 1px);
      background-size: 40px 40px;
      transform: perspective(500px) rotateX(60deg);
      animation: grid-move 15s linear infinite;
    }

    .lines {
      position: absolute;
      width: 100%;
      height: 100%;
      background:
        linear-gradient(90deg, transparent 0%, rgba(0, 255, 255, 0.2) 50%, transparent 100%);
      background-size: 200% 100%;
      animation: lines-move 3s linear infinite;
    }

    .glow {
      position: absolute;
      width: 100%;
      height: 100%;
      background: radial-gradient(circle at center, rgba(0, 150, 255, 0.3) 0%, transparent 70%);
    }
  }
}

.login-container {
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #00ffff;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.7);
  font-family: 'Orbitron', sans-serif;
  letter-spacing: 2px;
}

.login-form {
  border-radius: 6px;
  background: rgba(10, 20, 40, 0.8);
  backdrop-filter: blur(10px);
  width: 400px;
  padding: 25px 25px 5px 25px;
  border: 1px solid rgba(0, 255, 255, 0.3);
  box-shadow: 0 0 20px rgba(0, 255, 255, 0.3),
              inset 0 0 10px rgba(0, 255, 255, 0.1);

  .el-input {
    height: 38px;
    margin-bottom: 10px;

    input {
      height: 38px;
      background: rgba(0, 30, 60, 0.7);
      border: 1px solid rgba(0, 255, 255, 0.3);
      color: #e0e0ff;

      &:focus {
        border-color: #00ffff;
        box-shadow: 0 0 10px rgba(0, 255, 255, 0.5);
      }

      &::placeholder {
        color: rgba(150, 200, 255, 0.5);
      }
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
    color: #00ffff;
  }

  .el-checkbox {
    color: #00ffff;
  }
}

.cyber-button {
  background: linear-gradient(45deg, #0066ff, #00ffff);
  border: none;
  position: relative;
  overflow: hidden;
  z-index: 1;
  transition: all 0.3s;

  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
    transition: all 0.5s;
    z-index: -1;
  }

  &:hover:before {
    left: 100%;
  }

  &:hover {
    box-shadow: 0 0 15px rgba(0, 255, 255, 0.7);
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #00ffff;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
    border: 1px solid rgba(0, 255, 255, 0.3);
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #00ffff;
  font-family: 'Orbitron', sans-serif;
  font-size: 12px;
  letter-spacing: 1px;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.7);
}

.login-code-img {
  height: 38px;
}

@keyframes grid-move {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 40px 40px;
  }
}

@keyframes lines-move {
  0% {
    background-position: 0% 0%;
  }
  100% {
    background-position: 200% 0%;
  }
}
</style>

<style lang="scss" scoped>
.cyber-title {
  color: #00ffff;
  font-size: 36px;
  font-family: 'Orbitron', sans-serif;
  text-transform: uppercase;
  letter-spacing: 4px;
  margin-bottom: 5px;
  text-shadow: 0 0 10px rgba(0, 255, 255, 0.7),
               0 0 20px rgba(0, 255, 255, 0.5),
               0 0 30px rgba(0, 255, 255, 0.3);

  .blink {
    animation: blink 1s infinite;
  }
}

.subtitle {
  color: #00ffff;
  font-size: 16px;
  margin-bottom: 30px;
  letter-spacing: 2px;
  text-shadow: 0 0 5px rgba(0, 255, 255, 0.7);
}

.login-logo {
  text-align: center;
  padding: 20px 0;
  margin-bottom: 20px;
}

.link-type {
  color: #00ffff;
  text-decoration: none;

  &:hover {
    text-shadow: 0 0 10px rgba(0, 255, 255, 0.7);
  }
}

@keyframes blink {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0;
  }
}
</style>
